﻿using System;
using System.Data;
using System.Data.SqlClient;

namespace HRMService
{
    // NOTE: If you change the class name "GradeDetail" here, you must also update the reference to "GradeDetail" in App.config.
    public partial class MainService : IGradeDetail
    {
        public DataSet GradeDetail_GetAll()
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_GradeDetail_GetAll", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public int GradeDetail_Using(int GradeDetailID)
        {
            int retVal = 0;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_GradeDetail_Using", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@GradeDetailID", GradeDetailID);
            try
            {
                dbConn.Open();
                retVal = Convert.ToInt32(dbCmd.ExecuteScalar());
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public DataSet GradeDetail_GetAmount(int _GradeID, int _Level)
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_GradeDetail_GetAmount", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@GradeDetail_GradeID", _GradeID);
            dbCmd.Parameters.AddWithValue("@GradeDetail_LevelID", _Level);
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public DataSet GradeDetail_GetList(out int ItemCount, string branchcode, int currentPage, int pageSize, string keyWord, string orderBy, string orderDirection)
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_GradeDetail_GetList", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@BranchCode", branchcode);
            dbCmd.Parameters.AddWithValue("@CurrentPage", currentPage);
            dbCmd.Parameters.AddWithValue("@PageSize", pageSize);
            dbCmd.Parameters.AddWithValue("@KeyWord", keyWord);
            dbCmd.Parameters.AddWithValue("@OrderBy", orderBy);
            dbCmd.Parameters.AddWithValue("@OrderDirection", orderDirection);

            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
                ItemCount = Convert.ToInt32(retVal.Tables[1].Rows[0][0]);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public GradeDetailInfo GradeDetail_GetInfo(int GradeDetailID)
        {
            GradeDetailInfo retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_GradeDetail_GetInfo", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@GradeDetail_ID", GradeDetailID);
            SqlDataReader dr = null;
            try
            {
                dbConn.Open();
                dr = dbCmd.ExecuteReader();
                if (dr.Read())
                {
                    retVal = new GradeDetailInfo();
                    retVal.ID = Convert.ToInt32(dr["GradeDetail_ID"]);
                    retVal.GradeID = Convert.ToInt32(dr["GradeDetail_GradeID"]);
                    retVal.LevelID = Convert.ToInt32(dr["GradeDetail_LevelID"]);
                    retVal.Amount = Convert.ToDecimal(dr["GradeDetail_Amount"]);
                    retVal.BranchCode = dr["GradeDetail_BranchCode"].ToString();
                    retVal.MonthValue = Convert.ToInt32(dr["GradeDetail_Month"]);
                }
            }
            finally
            {
                if (dr != null) dr.Close();
                dbConn.Close();
            }
            return retVal;
        }

        public int GradeDetail_Insert(GradeDetailInfo eInfo)
        {
            int retVal = 0;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_GradeDetail_Insert", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@GradeDetail_GradeID", eInfo.GradeID);
            dbCmd.Parameters.AddWithValue("@GradeDetail_LevelID", eInfo.LevelID);
            dbCmd.Parameters.AddWithValue("@GradeDetail_Amount", eInfo.Amount);
            dbCmd.Parameters.AddWithValue("@GradeDetail_BranchCode", eInfo.BranchCode);
            dbCmd.Parameters.AddWithValue("@GradeDetail_Month", eInfo.MonthValue);
            try
            {
                dbConn.Open();
                retVal = Convert.ToInt32(dbCmd.ExecuteScalar());
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public bool GradeDetail_Update(GradeDetailInfo eInfo)
        {
            bool retVal = false;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_GradeDetail_Update", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@GradeDetail_ID", eInfo.ID);
            dbCmd.Parameters.AddWithValue("@GradeDetail_GradeID", eInfo.GradeID);
            dbCmd.Parameters.AddWithValue("@GradeDetail_LevelID", eInfo.LevelID);
            dbCmd.Parameters.AddWithValue("@GradeDetail_Amount", eInfo.Amount);
            dbCmd.Parameters.AddWithValue("@GradeDetail_BranchCode", eInfo.BranchCode);
            dbCmd.Parameters.AddWithValue("@GradeDetail_Month", eInfo.MonthValue);
            try
            {
                dbConn.Open();
                if (dbCmd.ExecuteNonQuery() > 0)
                {
                    retVal = true;
                }
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public bool GradeDetail_Delete(int GradeDetailID)
        {
            bool retVal = false;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_GradeDetail_Delete", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@GradeDetail_ID", GradeDetailID);
            try
            {
                dbConn.Open();
                if (dbCmd.ExecuteNonQuery() > 0)
                {
                    retVal = true;
                }
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
    }
}
